//
//  RotatingSphereViewController.m
//  SpecialEffectsProject
//
//  Created by 蓓蕾 on 2021/11/4.
//

#import "RotatingSphereViewController.h"
#import "DBSphereView.h"

@interface RotatingSphereViewController ()


@property (nonatomic, strong) NSArray *searchNameArray;
@property (nonatomic, retain) DBSphereView *sphereView;

@property (strong, nonatomic) UIScrollView *contentScrollView;

//@property (strong, nonatomic) UISearchBar *searchBar;

@end

@implementation RotatingSphereViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [self.view addSubview:self.contentScrollView];
    
    [self gainData];
    [self addShowTagView];
    [self initSubViews];
}

- (void)buttonPressed:(UIButton *)button
{
    [self touchActionJumpWithParams:button.titleLabel.text];
}

- (void)initSubViews
{
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(endEditSearchBar:)];
//    [self.contentScrollView addGestureRecognizer:tap];
//    UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"back_day"]];
//    self.view.backgroundColor = color;
    
//    self.searchBar.delegate = self;
}

- (void)gainData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"SearchNamePlist" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSString *string = dict[@"online_params"][@"KeyWord"];
    _searchNameArray = [string componentsSeparatedByString:@","];
}

- (void)addShowTagView
{
    CGFloat bounds_Width = MIN(kScreenWidth, (kScreenHeight - 49));
    _sphereView = [[DBSphereView alloc] initWithFrame:CGRectMake(0, 30, bounds_Width, bounds_Width)];
    self.contentScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(_sphereView.frame));
  
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSInteger i = 0; i < 50; i ++)
    {
        NSString *titleString = _searchNameArray[i * 4 + arc4random()% 4];
        NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:18.]};
        CGFloat textWidth = [titleString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size.width;
        textWidth += 10;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:titleString forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:18.];
        btn.frame = CGRectMake(0, 0, textWidth, 20);
//        btn.frame = CGRectMake(0, 0, 80, 20);
        [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [array addObject:btn];
        [_sphereView addSubview:btn];
    }
    [_sphereView setCloudTags:array];
    _sphereView.backgroundColor = [UIColor clearColor];
    [self.contentScrollView insertSubview:_sphereView atIndex:0];
}

- (void)endEditSearchBar:(UITapGestureRecognizer *)tap
{
//    [self.searchBar resignFirstResponder];
}

- (UIColor *)randomColor
{
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

#pragma mark - search delegate

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self touchActionJumpWithParams:searchBar.text];
}

- (void)touchActionJumpWithParams:(NSString*)params
{
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
